package com.hh.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hh.pojo.Area;
import com.hh.pojo.Demo;
import com.hh.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.groupingBy;

@Controller
@RequestMapping("/")
public class FlushController {
    private static List<Area> provinceList;
    private static List<Area> cityList;
    private static List<Area> countyList;
    private static List<User> userList;

    static {
        String provinceJson = "[{\"id\":\"1\",\"name\":\"北京\"},{\"id\":\"2\",\"name\":\"河北\"},{\"id\":\"3\",\"name\":\"河南\"}]";
        provinceList = new Gson().fromJson(provinceJson, new TypeToken<List<Area>>() {}.getType());
        String cityJson = "[{\"id\":\"10001\",\"name\":\"昌平\",\"pid\":\"1\"},{\"id\":\"10002\",\"name\":\"朝阳\",\"pid\":\"1\"},{\"id\":\"10003\",\"name\":\"海淀\",\"pid\":\"1\"},{\"id\":\"10004\",\"name\":\"石家庄\",\"pid\":\"2\"},{\"id\":\"10005\",\"name\":\"邯郸\",\"pid\":\"2\"},{\"id\":\"10006\",\"name\":\"郑州\",\"pid\":\"3\"},{\"id\":\"10007\",\"name\":\"洛阳\",\"pid\":\"3\"}]";
        cityList = new Gson().fromJson(cityJson, new TypeToken<List<Area>>() {}.getType());
        String countyJson = "[{\"id\":\"20001\",\"name\":\"昌平\",\"pid\":\"10001\"},{\"id\":\"20002\",\"name\":\"朝阳\",\"pid\":\"10002\"},{\"id\":\"20003\",\"name\":\"海淀\",\"pid\":\"10003\"},{\"id\":\"20004\",\"name\":\"桥西区\",\"pid\":\"10004\"},{\"id\":\"20005\",\"name\":\"桥东区\",\"pid\":\"10004\"},{\"id\":\"20006\",\"name\":\"丛台区\",\"pid\":\"10005\"},{\"id\":\"20007\",\"name\":\"邯山区\",\"pid\":\"10005\"},{\"id\":\"20008\",\"name\":\"金水区\",\"pid\":\"10006\"},{\"id\":\"20009\",\"name\":\"中原区\",\"pid\":\"10006\"},{\"id\":\"20010\",\"name\":\"西工区\",\"pid\":\"10007\"},{\"id\":\"20010\",\"name\":\"涧西区\",\"pid\":\"10007\"}]";
        countyList = new Gson().fromJson(countyJson, new TypeToken<List<Area>>() {}.getType());
        String userJson = "[{\"id\":\"1\",\"name\":\"张三\",\"province\":{\"id\":\"1\",\"name\":\"北京\"},\"city\":{\"id\":\"10001\",\"name\":\"昌平\",\"pid\":\"1\"},\"county\":{\"id\":\"20001\",\"name\":\"昌平\",\"pid\":\"10001\"}},{\"id\":\"2\",\"name\":\"李思\",\"province\":{\"id\":\"2\",\"name\":\"河北\"},\"city\":{\"id\":\"10005\",\"name\":\"邯郸\",\"pid\":\"2\"},\"county\":{\"id\":\"20006\",\"name\":\"丛台区\",\"pid\":\"10005\"}},{\"id\":\"3\",\"name\":\"王武\",\"province\":{\"id\":\"3\",\"name\":\"河南\"},\"city\":{\"id\":\"10006\",\"name\":\"郑州\",\"pid\":\"3\"},\"county\":{\"id\":\"20008\",\"name\":\"金水区\",\"pid\":\"10006\"}}]";
        userList = new Gson().fromJson(userJson, new TypeToken<List<User>>() {}.getType());
    }

    @RequestMapping("j/getProvinceList")
    public String getProvinceList(Model model) {
        model.addAttribute("provinceList", provinceList);
        return "jsp/province";
    }

    @RequestMapping(value = {"t/getCityList/{pid}","t/getCityList"})
    public String getCityList(@PathVariable(required = false,value = "pid") String pid, Model model) {
        model.addAttribute("cityList", cityList.stream().filter(s-> isBlank(pid) || s.getPid().equals(pid)).collect(Collectors.toList()));
        return "thymeleaf/city";
    }
    @RequestMapping(value = {"t/getSaveCityList/{pid}","t/getSaveCityList"})
    public String getSaveCityList(@PathVariable(required = false,value = "pid") String pid, Model model) {
        model.addAttribute("cityList", cityList.stream().filter(s->isBlank(pid) || s.getPid().equals(pid)).collect(Collectors.toList()));
        return "jsp/userSaveDialog";
    }
    @RequestMapping(value = {"f/getCountyList/{pid}","f/getCountyList"})
    public String getCountyList(@PathVariable(required = false,value = "pid") String pid, Model model) {
        model.addAttribute("countyList", countyList.stream().filter(s->isBlank(pid) || s.getPid().equals(pid)).collect(Collectors.toList()));
        return "freemarker/county";
    }
    @RequestMapping(value = {"f/getSaveCountyList/{pid}","f/getSaveCountyList"})
    public String getSaveCountyList(@PathVariable(required = false,value = "pid") String pid, Model model) {
        model.addAttribute("countyList", countyList.stream().filter(s->s.getPid().equals(pid)).collect(Collectors.toList()));
        return "jsp/userSaveDialog";
    }
    @RequestMapping("t/userList")
    public String userList( @RequestParam(required = false,value = "id") String id,
                       @RequestParam(required = false,value = "name") String name,
                       @RequestParam(required = false,value = "province") String province,
                       @RequestParam(required = false,value = "city") String city,
                       @RequestParam(required = false,value = "county") String county,
                       Model model) {

        List<User> result  = userList.stream().filter(s -> isBlank(id) || s.getId().equals(id))
                .filter(s -> isBlank(name) || s.getName().equals(name))
                .filter(s -> isBlank(province) || s.getProvince().getId().equals(province))
                .filter(s -> isBlank(city) || s.getCity().getId().equals(city))
                .filter(s -> isBlank(county) || s.getCounty().getId().equals(county)).toList();
        model.addAttribute("list", result);
        return "thymeleaf/list";
    }
    @RequestMapping("t/userDetail")
    public String userList( @RequestParam(required = false,value = "id") String id,
                       Model model) throws Exception {
        if(isBlank(id)){
            throw new Exception("缺少id");
        }

        List<User> result  = userList.stream().filter(s -> s.getId().equals(id)).toList();
        model.addAttribute("user", result.get(0));
        return "freemarker/detail";
    }
    @RequestMapping("j/toSave")
    public String toSave(Model model) throws Exception {
        model.addAttribute("provinceList", provinceList);
        return "jsp/userSaveDialog";
    }

    @RequestMapping("j/doSave")
    public String doSave(@RequestBody Map<String,String> params,Model model) throws Exception {

        String name = params.get("name");
        String province = params.get("province");
        String provinceName = params.get("provinceName");
        String city = params.get("city");
        String cityName = params.get("cityName");
        String county = params.get("county");
        String countyName = params.get("countyName");


        User user = new User(String.valueOf(userList.size()+1),name,new Area(province,provinceName),new Area(city,cityName),new Area(county,countyName));
        userList.add(user);

        model.addAttribute("list", userList);
        return "thymeleaf/list";
    }

    public static boolean isBlank(String s) {
        int strLen = s == null ? 0 : s.length();
        return strLen == 0;
    }
}
